{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import os\n",
    "import sys\n",
    "# 导入自定义的推荐系统的包\n",
    "sys.path.append(r\"D:\\_俊\\_pythonproject\\myproject\\machine learning\\mycode\\recommed\")\n",
    "from processing import *\n",
    "from metric import *\n",
    "from recommend import *\n",
    "import numpy as np\n",
    "import time\n",
    "from collections import Counter\n",
    "\n",
    "def my_test(data, recommend_func, N = 20, M = 8, desc = True):\n",
    "    #print(kwargs, kwargs['max_iters'])\n",
    "    # 测试数据\n",
    "    start0 = time.time()\n",
    "    recall = precision = cover = popular = deversity = 0\n",
    "    for k in range(M):\n",
    "        start = time.time()\n",
    "        train, test = SpliceData(data, M, k, seed = 1234)\n",
    "        train_dic = list2dict(train[:,:-1])\n",
    "        test_dic = list2dict(test[:,:-1])\n",
    "        item_count = Counter(i for _, i,_ in train)\n",
    "        recommend = recommend_func(N)   # 随机推荐\n",
    "        recommend.fit(train)\n",
    "        recall_, precision_ = recall_precision(train_dic, test_dic, recommend)\n",
    "        coverage_ = coverage(train_dic, test_dic, recommend)\n",
    "        popularity_ = popularity(train_dic, test_dic, recommend, item_count)\n",
    "        deversity_ = Diversity(train_dic, test_dic, recommend)\n",
    "        recall += recall_\n",
    "        precision += precision_\n",
    "        cover += coverage_\n",
    "        popular += popularity_\n",
    "        deversity += deversity_\n",
    "        if desc:\n",
    "            print(\"epoch: {}, 召回率: {:.6f}, 精确率: {:.6f}, 覆盖率: {:.6f}, 流行度: {:.6f}, 多样性: {:.6f} =============== 用时{:.2f}s\".format(k+1, recall_, \n",
    "                                                                                     precision_, coverage_, popularity_, deversity_, time.time()-start))\n",
    "    if desc:\n",
    "        print(\"\\n\")\n",
    "    print(\"[{}] K: {:3d}, 召回率: {:.6f}, 精确率: {:.6f}, 覆盖率: {:.6f}, 流行度: {:.6f}, 多样性: {:.6f} =============== 用时{:.2f}s\".format(recommend_func.__name__, N, recall / M, \n",
    "                                                                                     precision / M, cover / M, popular / M, deversity / M, time.time()-start0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((405665, 3), (409220, 3))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机数据集\n",
    "# 构造10000条数据\n",
    "Delicious = generate_label(11200,8791,42233,405665, label_p = 0.05, l_p = 0.4)\n",
    "CiteULike = generate_label(12466, 7318, 23068, 409220, label_p = 0.1, l_p = 0.3)\n",
    "Delicious.shape, CiteULike.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 1, 召回率: 0.002446, 精确率: 0.000559, 覆盖率: 0.999772, 流行度: 3.759899, 多样性: 0.012889 =============== 用时101.34s\n",
      "epoch: 2, 召回率: 0.001834, 精确率: 0.000420, 覆盖率: 0.999886, 流行度: 3.759332, 多样性: 0.012885 =============== 用时101.02s\n",
      "epoch: 3, 召回率: 0.002052, 精确率: 0.000469, 覆盖率: 0.999886, 流行度: 3.759902, 多样性: 0.012930 =============== 用时103.25s\n",
      "epoch: 4, 召回率: 0.002426, 精确率: 0.000556, 覆盖率: 0.999772, 流行度: 3.758910, 多样性: 0.012920 =============== 用时101.56s\n",
      "epoch: 5, 召回率: 0.002170, 精确率: 0.000497, 覆盖率: 0.999659, 流行度: 3.759600, 多样性: 0.012967 =============== 用时100.54s\n",
      "epoch: 6, 召回率: 0.002091, 精确率: 0.000478, 覆盖率: 0.999886, 流行度: 3.759495, 多样性: 0.012906 =============== 用时100.47s\n",
      "epoch: 7, 召回率: 0.001953, 精确率: 0.000447, 覆盖率: 0.999886, 流行度: 3.758883, 多样性: 0.012911 =============== 用时104.10s\n",
      "epoch: 8, 召回率: 0.002209, 精确率: 0.000506, 覆盖率: 0.999772, 流行度: 3.759391, 多样性: 0.012925 =============== 用时100.74s\n",
      "\n",
      "\n",
      "[LabelRecommed] K:  20, 召回率: 0.002148, 精确率: 0.000491, 覆盖率: 0.999815, 流行度: 3.759426, 多样性: 0.012917 =============== 用时813.02s\n"
     ]
    }
   ],
   "source": [
    "my_test(Delicious, LabelRecommed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
